package com.action.utils;//package com.kachemama.core.util;

import android.content.Context;
import android.os.AsyncTask;

import com.base.app_core.model.params.UserClientTableParams;
import com.base.app_core.service.pressenter.XYBUPressenter;
import com.base.app_core.service.user.model.User;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.Preferences;
import com.base.app_core.util.log.F;
import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;

import javax.inject.Inject;
import javax.inject.Singleton;


/**
 * Created by jiansheng.li on 2017/5/25.
 */
@Singleton
public class LocUtils implements TencentLocationListener {

    @Inject
    User user;

    short mTime = 0;

    @Inject
    UserClientTableParams userClientTableParams;

    XYBUPressenter XYBUPressenter = null;

    public void setXYBUPressenter(XYBUPressenter XYBUPressenter) {
        this.XYBUPressenter = XYBUPressenter;
    }

    private LocationLisener l;

    @Inject
    public LocUtils() {

    }

    public short getmTime() {
        return mTime;
    }

    public void setmTime(short mTime) {
        this.mTime = mTime;
    }

    public void requestLocation(Context context) {
        TencentLocationRequest tlr = TencentLocationRequest.create();
        tlr.setAllowCache(true);
        tlr.setRequestLevel(TencentLocationRequest.REQUEST_LEVEL_ADMIN_AREA);
        tlr.setInterval(60 * 1000);
        TencentLocationManager locationManager = TencentLocationManager.getInstance(context);
        int error = locationManager.requestLocationUpdates(tlr, this);
        F.j("定位请求错误==>" + error);
    }


    @Override
    public void onLocationChanged(TencentLocation location, int error, String reason) {
        if (TencentLocation.ERROR_OK == error) {
            if (null != l) {
                l.onLocationSuccess(location);
            }
            doLocationSaveOnBackGround(location);
            postLoc();
        } else {
            if (l != null) {
                l.onLocationFailure(error, reason);
            }
        }
    }

    private void doLocationSaveOnBackGround(final TencentLocation location) {
        new AsyncTask<TencentLocation, Void, Void>() {
            @Override
            protected Void doInBackground(TencentLocation... tencentLocations) {
                user.setLng(location.getLongitude());
                user.setLat(location.getLatitude());
                String cityCode = location.getCityCode();
                if (cityCode.length() == 6) {
                    String proCode = cityCode.substring(0, 2);
                    String cityCOde = cityCode.substring(2, 4);
                    String areacode = cityCode.substring(4, 6);
                    user.setProvinceCode(proCode);
                    user.setCityCode(cityCOde);
                    user.setAreaCode(areacode);
                    Preferences.putString(AppConstants.SP.PROVINCE_COODE, proCode);
                    Preferences.putString(AppConstants.SP.CITY_COODE, cityCOde);
                    Preferences.putString(AppConstants.SP.AREA_COODE, areacode);
                }
                Preferences.putString(AppConstants.SP.LAT, String.valueOf(location.getLatitude()));
                Preferences.putString(AppConstants.SP.LNG, String.valueOf(location.getLongitude()));
                return null;
            }
        }.execute(location);
    }

    public void postLoc() {
        if (user == null) {
            return;
        }
        if (null != XYBUPressenter) {
            Double lng = user.getLng();
            if (lng == null) {
                return;
            }
            Double lat = user.getLat();
            if (lat == null) {
                return;
            }
            if ((mTime++) % 10 == 0) {
                XYBUPressenter.postUserLoc(lng, lat,
                        AppConstants.getPushTag(), PhoneUtil.getMobileType(),
                        user.getProvinceCode(), user.getCityCode(), user.getAreaCode());
            }
            if (mTime >= 254)
                mTime = 0;

        }
    }

    @Override
    public void onStatusUpdate(String s, int i, String s1) {
        F.j(s);
    }

    public void setOnLocationLisener(LocationLisener l) {
        this.l = l;
    }

    public interface LocationLisener {

        void onLocationSuccess(TencentLocation location);

        void onLocationFailure(int error, String reason);
    }
}
